 <#include "/common.include"/>
package ${packageName}.${moduleName}.service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;
import com.sun.corba.se.spi.orb.StringPair;
import com.yhsarchives.common.dao.PaginatedHelper;
import com.yhsarchives.common.easyui.EasyuiGridResult;
import com.yhsarchives.common.exception.VOCastException;
import com.yhsarchives.common.service.BaseService;
import com.yhsarchives.common.util.DateTimeUtil;
import com.yhsarchives.common.util.VoUtil;
import com.yhsarchives.common.vo.CommonResultVo;
import com.yhsarchives.entitys.T${className}Bean;

import ${packageName}.${moduleName}.vo.${className}Vo;


@Service
public class ${className}Service extends BaseService{
	
	//改为sql式的
	public EasyuiGridResult getPageList(PaginatedHelper paginatedHelper, ${className}Vo ${classNameFirstLower}Vo){
		StringBuffer whereBy=new StringBuffer("where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		<#list table.columns as column>
			<#if column.isDateTimeColumn>
			try{
				if(StringUtil.isNotNull(${classNameFirstLower}Vo.get${column.columnName}Start())){
					whereBy.append(" and ${column.columnNameFirstLower} >= ?");
					params.add(DateTimeUtil.parseToDate(${classNameFirstLower}Vo.get${column.columnName}Start(), "yyyy-MM-dd"));
				}
				if(StringUtil.isNotNull(${classNameFirstLower}Vo.get${column.columnName}End())){
					whereBy.append(" and ${column.columnNameFirstLower} <= ?");
					params.add(DateTimeUtil.parseToDate(${classNameFirstLower}Vo.get${column.columnName}End(), "yyyy-MM-dd"));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			</#if>
			<#if column.isNumberColumn>
			if(${classNameFirstLower}Vo.get${column.columnName}()!=null){
				whereBy.append(" and ${column.columnNameFirstLower}=? ");
				params.add(${classNameFirstLower}Vo.get${column.columnName}());
			}
			</#if>
			<#if column.isStringColumn>
			if(!StringUtils.isNullOrEmpty(${classNameFirstLower}Vo.get${column.columnName}())){
				whereBy.append(" and ${column.columnNameFirstLower} like '%"+${classNameFirstLower}Vo.get${column.columnName}()+"%' ");
			}
			</#if>
		</#list>

		String hql="select count(*) from T${className}Bean "+whereBy;
		int count=super.getCount(hql, params.toArray());
		
		hql="from T${className}Bean "+whereBy+" order by ${pkColumnNameFirstLower} desc";
		List<T${className}Bean> ${classNameFirstLower}s = super.getList(hql, params.toArray(), paginatedHelper);
		List<${className}Vo> ${classNameFirstLower}Vos = new ArrayList<${className}Vo>();
		for(int i=0;i<${classNameFirstLower}s.size();i++){
			T${className}Bean t${className}Bean = ${classNameFirstLower}s.get(i);
			${className}Vo ${classNameFirstLower}Vo2 = new ${className}Vo();
			
				
				try {
					//PropertyUtils.copyProperties(pVo, tpbBean);
					VoUtil.copyValuesNotNull(t${className}Bean,${classNameFirstLower}Vo2,false);
				} catch (VOCastException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			${classNameFirstLower}Vos.add(${classNameFirstLower}Vo2);
		}
		return new EasyuiGridResult(${classNameFirstLower}Vos, count);
	}
	
	
	@Transactional
	public CommonResultVo save(${className}Vo ${classNameFirstLower}Vo) throws VOCastException, IOException{
		//鍚嶇О锛岀紪鍙凤紝浜у湴锛岃鏍硷紝鍗曚綅
		String[] keys = {
		<#list table.columns as column>
		"${column.columnNameFirstLower}",
		</#list>
		};
		String[] values = {
		<#list table.columns as column>
			"${classNameFirstLower}Vo.get${column.columnName}",
		</#list>
		};
		if(isFieldExist(T${className}Bean.class, keys,values,"${pkColumnNameFirstLower}",${classNameFirstLower}Vo.get${pkColumnName}())){
			return new CommonResultVo(false, "瀛樺湪閲嶅鐨�{webNameCN}锛�);
		}
		T${className}Bean ${classNameFirstLower} = new T${className}Bean();//product
		if(${classNameFirstLower}Vo.get${pkColumnName}()==null){
			VoUtil.copyValuesNotNull(${classNameFirstLower}Vo, ${classNameFirstLower},false); //productVo product
			//纭紪鐮侊紝瑕佹眰姣忎釜閮界敤濡備笅鍛藉悕
			${classNameFirstLower}.setCreaterId(super.getLoginUserId());
			${classNameFirstLower}.setCreaterTime(DateTimeUtil.getNowTimeStamp());
			${classNameFirstLower}.setUpdaterId(super.getLoginUserId());
			${classNameFirstLower}.setUpdaterTime(DateTimeUtil.getNowTimeStamp());
		}else{
			${classNameFirstLower}=getTarget(T${className}Bean.class, ${classNameFirstLower}Vo.get${pkColumnName}());
			VoUtil.copyValuesNotNull(${classNameFirstLower}Vo, ${classNameFirstLower}, false);
			
			${classNameFirstLower}.setUpdaterId(super.getLoginUserId());
			${classNameFirstLower}.setUpdaterTime(DateTimeUtil.getNowTimeStamp());
		}
		super.saveOrUpdate(${classNameFirstLower});
		return new CommonResultVo(true, "淇濆瓨鎴愬姛!");
	}
	
	/**
	 * 鍒犻櫎${webNameCN}
	 * @param serviceId
	 */
	
	@Transactional
	public void delete(${pkColumnType} ${pkColumnNameFirstLower}){
		super.deleteTarget(T${className}Bean.class, "${pkColumnNameFirstLower}", ${pkColumnNameFirstLower});
	}
	
	
	
	
	/**
	 * ${webNameCN}鏄惁宸茶浣跨敤
	 */
	public boolean isUsed(${pkColumnType} ${pkColumnNameFirstLower}){
		return false;
		/*int count=super.getCount(TStockOrderProductBean.class, "productId", productId);
		int count2 = super.getCount(TOutStockOrderProductBean.class, "stockOrderProductId", productId);
		return (count+count2)>0;*/
	}
	

}
